---@class UIArenaRank : UIBase
UIArenaRank = UIBase.New(PanelResNames.UIArenaRank)
local t = UIArenaRank

function t.Init( _arena_type ) -- _arena_type 竞技场类型
    -- print("UIArenaRank------>", _arena_type)
    t.cur_arena_type = _arena_type or EnumConst.ArenaBattleType.Normal
    UIBase.Init(t)
end

function t:OnCreate()
    self.txt_title = self:GetChild("title")
    self.btnClose = self:GetChild("btnClose")
    self.bgClose = self:GetChild("bgClose")
    self.rankList = self:GetList("ranklist/ranklist")
    self.rankList2 = self:GetList("ranklist2/ranklist")
    self.my = self:GetChild("ranklist/my")
    self.my2 = self:GetChild("ranklist2/my2")
    self:RegisterOnClick(self.btnClose, self.ClickClose)
    self:RegisterOnClick(self.bgClose, self.ClickClose)

    self:SetDynamicList(self.rankList, self.ItemRender)
    self:SetDynamicList(self.rankList2, self.ItemRender2)
    self.rankList:SetVirtual()
    self.rankList2:SetVirtual()
    self.poolMgr = nil
    
    self.datas = nil
    self:RegisterOnClick(self:GetChild("btnReward"),self.ShowPanel) 

    if t.effect_mat == nil then
        t.effect_mat = Util.SetSkeletonAnimMatrialStencil(self.my, 3)
    end

    local ctrl_type = self:GetController("type")
    if t.cur_arena_type == EnumConst.ArenaBattleType.Normal then
        self.poolMgr = ComponentPoolMgr.New(t.RankItem)
        self.myItem = self.poolMgr:GetBindScript(self.my)
        ctrl_type:SetSelectedIndex(0)
        if self.txt_title then
            self.txt_title.text = "竞技场排行榜"--getLanguage("RogueTips05")
        end
        self:RegisterEvent(Config.EventType.Fresh_ArenaRanks, self.RefreshWithData)
        self:RegisterEvent(Config.EventType.FreashRoleInfoOther, self.GotOtherShowInfo)
        logicMgr.ArenaManager.requestRankInfo()
    elseif t.cur_arena_type == EnumConst.ArenaBattleType.Cross then
        self.poolMgr = ComponentPoolMgr.New(t.RankItem)
        self.myItem = self.poolMgr:GetBindScript(self.my)
        ctrl_type:SetSelectedIndex(0)
        if self.txt_title then
            self.txt_title.text = "跨服竞技场排行榜"--getLanguage("RogueTips05")
        end
        self:RegisterEvent(Config.EventType.Fresh_CrossArenaRanks, self.RefreshWithData)
        self:RegisterEvent(Config.EventType.FreashRoleInfoOther, self.GotOtherShowInfo);
        logicMgr.ArenaManager.requestCrossRankInfo()
    
    elseif t.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        self.poolMgr = ComponentPoolMgr.New(t.RankItem2)
        self.myItem = self.poolMgr:GetBindScript(self.my2)
        ctrl_type:SetSelectedIndex(1)
        if self.txt_title then
            self.txt_title.text = "多队跨服竞技场排行榜"--getLanguage("RogueTips05")
        end
        self:RegisterEvent(Config.EventType.Fresh_CrossTeamArenaRanks, self.RefreshWithData)
        self:RegisterEvent(Config.EventType.FreashRoleInfoOther, self.GotOtherShowInfo)
        logicMgr.ArenaManager.requestCrossTeamRankInfo()
    else
        error("错误竞技场类型")
    end 

end

function t.ShowPanel()
    if t.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        showUI(PanelResNames.UIReward)
    else
        showUI(PanelNames.UIArenaRankReward, t.cur_arena_type)
    end
end

function t:OnEnable()  
    if t.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        self.rankList2:ScrollToView(0)
    else
        self.rankList:ScrollToView(0)
    end
end

-- function t:OnDisable()
-- end

function t:OnDestroy()
    if t.cur_arena_type == EnumConst.ArenaBattleType.Normal then
        self:UnRegisterEvent(Config.EventType.FreashRoleInfoOther, self.GotOtherShowInfo)
        self:UnRegisterEvent(Config.EventType.Fresh_ArenaRanks, self.RefreshWithData)
    elseif t.cur_arena_type == EnumConst.ArenaBattleType.Cross then
        self:UnRegisterEvent(Config.EventType.FreashRoleInfoOther, self.GotOtherShowInfo)
        self:UnRegisterEvent(Config.EventType.Fresh_CrossArenaRanks, self.RefreshWithData)
    elseif t.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        self:UnRegisterEvent(Config.EventType.FreashRoleInfoOther, self.GotOtherShowInfo)
        self:UnRegisterEvent(Config.EventType.Fresh_CrossTeamArenaRanks, self.RefreshWithData)
    else
        error("错误竞技场类型")
    end

    if self.poolMgr then
        self.poolMgr:OnDestroy()
    end
    self.datas = nil
    self.poolMgr = nil
    self.myItem = nil

    self.txt_title = nil
    self.btnClose = nil
    self.bgClose = nil
    self.rankList = nil
    self.my = nil 

    t.cur_arena_type = nil

    if t.effect_mat then
        for i=0, t.effect_mat.Length - 1 do
            t.effect_mat:GetValue(i):SetFloat("_StencilComp", 8)
        end
        t.effect_mat = nil
    end
end

function t:Refresh()
    if self.datas and self.datas.ranks then
        local num = #self.datas.ranks
        if num < 10 then
            num = #self.datas.ranks + (10 -#self.datas.ranks)
        elseif num > Config.Const_Rank_Max_Num then
            num = Config.Const_Rank_Max_Num
        end
        if self.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then

            self.rankList2.numItems = Config.Const_Rank_Max_Num
            HelperFunc.ListTranstionPlay(self.rankList2)
            self.myItem:Refresh(self.datas.myRank)
        else
            self.rankList.numItems = Config.Const_Rank_Max_Num--TODO RANK
            HelperFunc.ListTranstionPlay(self.rankList)
            self.myItem:Refresh(self.datas.myRank, t.cur_arena_type)
        end
    else
        if self.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
            self.rankList2.numItems = 10
            HelperFunc.ListTranstionPlay(self.rankList2)
        else
            self.rankList.numItems = 10
            HelperFunc.ListTranstionPlay(self.rankList)
        end
    end
    if self.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        self.rankList2:ScrollToView(0)
    else
        self.rankList:ScrollToView(0)
    end 
end

function t:ItemRender(index, go)
    local data = self.datas.ranks[index]
    local item = self.poolMgr:GetBindScript(go)
    item:Refresh(data, t.cur_arena_type)

    if t.effect_mat == nil then
        t.effect_mat = Util.SetSkeletonAnimMatrialStencil(go, 3)
    end
end

function t:ItemRender2(index, go)
    local data = self.datas.ranks[index]
    local item = self.poolMgr:GetBindScript(go)
    item:Refresh(data)
end

function t:ClickClose()
    closeUI(PanelResNames.UIArenaRank)
end

function t:GotOtherShowInfo(info)
    if t.effect_mat == nil then
        t.effect_mat = Util.SetSkeletonAnimMatrialStencil(self.my, 3)
    end

    local cur_arena_type = t.cur_arena_type
    showUI(PanelNames.UIPlayerShow, info,nil,false,function()
        UIBottom.enterAdventure()
        showUI(PanelNames.UIArenaMainEnter)
        showUI(PanelNames.UIArenaMain, cur_arena_type)
        showUI(PanelNames.UIArenaRank, cur_arena_type)
    end)
end


function t:RefreshWithData()
    if t.cur_arena_type == EnumConst.ArenaBattleType.Normal then
        self.datas = logicMgr.ArenaManager.getRankInfo()
    elseif t.cur_arena_type == EnumConst.ArenaBattleType.Cross then
        self.datas = logicMgr.ArenaManager.getCrossRankInfo()
    elseif t.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        self.datas = logicMgr.ArenaManager.getCrossTeamRankInfo()
    else
        error("错误竞技场类型")
    end
    t:Refresh()
end

t.RankItem = Class("rankItem", UIComponentInterface)
---@class UIArenaRank_Item : UIComponentInterface
local t_t = t.RankItem
function t_t:OnCreate()
    self.rankText = self:GetChild("rank")
    self.txt_point_label = self:GetChild("textPoint")
    self.pointText = self:GetChild("point")
    self.nameText = self:GetChild("name")
    self.lvText = self:GetChild("level")
    self.classIcon = self:GetChild("Icon")
    self.icon = self:GetChild("head/head/HeadIcon")
    self.frame = self:GetChild("head/frame")
    self.head = self:GetChild("head");
    self.ctrlNoRank = self:GetController("ctrlNoRank")
    self.ctrlRank = self:GetController("rank")
    self.ctrlShow = self:GetController("c1")
    self.head=self:GetChild("head")
    self.server_name_controller = self:GetController("ServerName")
    self.txt_server_name = self:GetChild("textSevers") 

    self.classIconEffect = self:GetChild("Effect_Dwsg");
end
function t_t:OnDestroy()
    self.data = nil
end
function t_t:Refresh(data, _arena_type)
    if data == nil then
        self.ctrlShow.selectedIndex = 1
        self.classIconEffect.visible = false
        return
    end
    self.classIconEffect.visible = true
    self.data = data
    if _arena_type == EnumConst.ArenaBattleType.Normal then
        self.server_name_controller:SetSelectedIndex(0)
    elseif _arena_type == EnumConst.ArenaBattleType.Cross then
        self.server_name_controller:SetSelectedIndex(1) 
        local sever_data = logicMgr.LoginManager.GetServerInAll(self.data.serverId)
        if sever_data ~= nil then
            self.txt_server_name.text = sever_data[Server_ENU_INDEX.NAME] 
        else
            self.txt_server_name.text = "未知"
        end
    elseif _arena_type == EnumConst.ArenaBattleType.TeamCross then
        self.server_name_controller:SetSelectedIndex(1) 
        local sever_data = logicMgr.LoginManager.GetServerInAll(self.data.serverId)
        if sever_data ~= nil then
            self.txt_server_name.text = sever_data[Server_ENU_INDEX.NAME] 
        else
            self.txt_server_name.text = "未知"
        end
    else
        error("错误竞技场类型")
    end
    if data.rank <= 0 then
        self.ctrlNoRank.selectedIndex = 1;
        self.ctrlRank.selectedIndex = 3;
    else
        if self.ctrlShow.selectedIndex ~= 2 then
            self.ctrlShow.selectedIndex = 0
        end
        self.ctrlNoRank.selectedIndex = 0
        self.ctrlRank.selectedIndex = data.rank >= 4 and 3 or (data.rank - 1);
        if _arena_type == EnumConst.ArenaBattleType.TeamCross then
            if data.rank > 50 then
                self.ctrlNoRank.selectedIndex = 1
                self.ctrlRank.selectedIndex = 3
            end
        end
    end
    
    if data.rank==1 then
        self.head:GetController("first").selectedIndex=1
    else
        self.head:GetController("first").selectedIndex=0
    end
    self.rankText.text = data.rank
    
    self.nameText.text = data.name
    self.lvText.text = data.level
    local gradingCfg = nil
    if _arena_type == EnumConst.ArenaBattleType.TeamCross then
        gradingCfg = logicMgr.ArenaManager.getAthleticGradingConfigByRank(data.rank)
        self.txt_point_label.text = getLanguage(gradingCfg.f_Name)
        self.pointText.text = ""
    else
        gradingCfg = logicMgr.ArenaManager.getLevelConfigByScore(data.score, _arena_type)
        self.pointText.text = data.score
        self.txt_point_label.text = "积分"
        if self.classIconEffect ~= nil then
            local level_num = gradingCfg.f_Id
            self.classIconEffect.animationName = "idle" .. level_num
            self.classIconEffect.skinName = level_num
            self.classIconEffect.loop = true
        end
    end
    self.classIcon.url = EnumConst.AtlasPath.ICONAtlas..tostring(gradingCfg and gradingCfg.f_Icon)
    self:SetPlayerIcon(self.icon, data.headId, self.frame, data.headFrame) -- 头像框先写死,需要后端发
    self.head.onClick:Clear();
    self.head.onClick:Add(function()
        if tostring(data.playerId) ~= logicMgr.HeroManager.GetRoleId() then
            roleInfoHandler.sendreqOtherRoleInfo(data.playerId);
        end

    end)
end


t.RankItem2 = Class("rankItem2", UIComponentInterface)
---@class UIArenaRank_Item : UIComponentInterface
local t_t2 = t.RankItem2
function t_t2:OnCreate()

    self.ctrlShow = self:GetController("c1")
    self.ctrlRank = self:GetController("rank")
    self.server_name_controller = self:GetController("ServerName")

    self.nameText = self:GetChild("name")
    self.head = self:GetChild("head")
    self.icon = self:GetChild("head/head/HeadIcon")
    self.frame = self:GetChild("head/frame")
    self.classIcon = self:GetChild("Icon")
    self.txt_server_name = self:GetChild("textSevers")  
    self.classIconEffect = self:GetChild("Effect_Dwsg")
    self.lvText = self:GetChild("level")
    self.txt_rank_name = self:GetChild("Rank")
  
end
function t_t2:OnDestroy()
    self.data = nil
end
function t_t2:Refresh(data)
    if data == nil then
        self.ctrlShow:SetSelectedIndex( 1 )
        self.classIconEffect.visible = false
        return
    end

    self.classIconEffect.visible = true
    self.data = data
 
    self.server_name_controller:SetSelectedIndex(1) 
    local sever_data = logicMgr.LoginManager.GetServerInAll(self.data.serverId)
    if sever_data ~= nil then
        self.txt_server_name.text = sever_data[Server_ENU_INDEX.NAME] 
    else
        self.txt_server_name.text = "未知"
    end
 
    if data.rank <= 0 then 
        self.ctrlRank:SetSelectedIndex( 3 )
    else
        if self.ctrlShow.selectedIndex ~= 2 then
            self.ctrlShow:SetSelectedIndex( 0 )
        end
        self.ctrlRank:SetSelectedIndex(data.rank >= 4 and 3 or (data.rank - 1) )
    end
    
    if data.rank == 1 then
        self.head:GetController("first"):SetSelectedIndex( 1 )
    else
        self.head:GetController("first"):SetSelectedIndex( 0 )
    end
    self.nameText.text = data.name
    self.lvText.text = data.level
    local gradingCfg, cfg_matching = logicMgr.ArenaManager.getAthleticGradingConfigByRank(data.score)
    if self.classIconEffect ~= nil then
        local level_num = gradingCfg.f_RankId
        self.classIconEffect.animationName = "idle" .. level_num
        self.classIconEffect.skinName = level_num
        self.classIconEffect.loop = true
    end
    
    self.classIcon.url = EnumConst.AtlasPath.ICONAtlas..tostring(gradingCfg and gradingCfg.f_Icon)
    self.txt_rank_name.text = getLanguage(cfg_matching.f_RankName)
    self:SetPlayerIcon(self.icon, data.headId, self.frame, data.headFrame) -- 头像框先写死,需要后端发
    self.head.onClick:Clear()
    self.head.onClick:Add(function()
        if tostring(data.playerId) ~= logicMgr.HeroManager.GetRoleId() then
            roleInfoHandler.sendreqOtherRoleInfo(data.playerId);
        end

    end)
end

return t